/*************************************************************************************************
* 文件名 	：bsp_gpio
* 作者	  ：菜菜的菠萝头
* 版本	  ：V1.0.0
* 日期	  ：2024/09.08
* 文件作用：IO初始化
*
**************************************************************************************************
* 注意事项：
*1、无。
*************************************************************************************************/
#include "bsp_gpio.h"

static void GpioBspWkup(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct = {0};
	FL_EXTI_InitTypeDef extiInitStruct = {0};
	FL_EXTI_CommonInitTypeDef  extiCommonInitStruct = {0};
	
	GPIO_InitStruct.mode = FL_GPIO_MODE_INPUT;
	GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull = FL_ENABLE;
	GPIO_InitStruct.remapPin = FL_DISABLE;
	GPIO_InitStruct.pin = PIN_WKUP0;
	
	FL_GPIO_Init(GPIO_WKUP0, &GPIO_InitStruct );
	
	FL_CMU_EnableEXTIOnSleep();//休眠使能外部中断采样

	extiCommonInitStruct.clockSource = FL_CMU_EXTI_CLK_SOURCE_HCLK;    //EXTI中断采样时钟选择
	(void)FL_EXTI_CommonInit(&extiCommonInitStruct);

	extiInitStruct.filter      = FL_ENABLE;                          //使能数字滤波
	extiInitStruct.triggerEdge = FL_GPIO_EXTI_TRIGGER_EDGE_FALLING;   //设置触发边沿
	
	extiInitStruct.input       = FL_GPIO_EXTI_INPUT_GROUP3;          //中断线上对应的IO
	(void)FL_EXTI_Init(FL_GPIO_EXTI_LINE_3, &extiInitStruct);
	
	/*NVIC中断配置*/
	NVIC_DisableIRQ(GPIO_IRQn);
	NVIC_SetPriority(GPIO_IRQn, 2); //中断优先级配置
	NVIC_EnableIRQ(GPIO_IRQn);
}

static void GpioBspLed(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct;
	
	FL_GPIO_StructInit(&GPIO_InitStruct);
	GPIO_InitStruct.analogSwitch = FL_DISABLE;
	GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT;
	GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull = FL_DISABLE;
	GPIO_InitStruct.remapPin = FL_DISABLE;
	
	//Led脚
	GPIO_InitStruct.pin = PIN_LED;
	FL_GPIO_Init(GPIO_LED,&GPIO_InitStruct); 					//初始化GPIO
	FL_GPIO_ResetOutputPin(GPIO_LED,PIN_LED);
}

static void GpioBspUartInit(void)
{    
	FL_GPIO_InitTypeDef GPIO_InitStruct = {0};

	GPIO_InitStruct.mode          = FL_GPIO_MODE_DIGITAL;
	GPIO_InitStruct.outputType    = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull          = FL_ENABLE;
	GPIO_InitStruct.remapPin      = FL_DISABLE;
	GPIO_InitStruct.analogSwitch  = FL_DISABLE;
	
	GPIO_InitStruct.pin           = PIN_UART5_RXD;
	(void)FL_GPIO_Init(GPIO_UART5_RXD, &GPIO_InitStruct);

	GPIO_InitStruct.mode          = FL_GPIO_MODE_DIGITAL;
	GPIO_InitStruct.outputType    = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull          = FL_DISABLE;
	GPIO_InitStruct.remapPin      = FL_DISABLE;
	GPIO_InitStruct.analogSwitch  = FL_DISABLE;
	
	GPIO_InitStruct.pin           = PIN_UART5_TXD;
	(void)FL_GPIO_Init(GPIO_UART5_TXD, &GPIO_InitStruct);
}



/*************************************************************************************************
*函数名称		：GpioBspInit
*功能说明		：IO初始化
*形参 		：无
*返回值		：无
*其他 		：无
*************************************************************************************************/
void GpioBspInit(void)
{
	GpioBspWkup();
	GpioBspLed();
	GpioBspUartInit();
}

//LED控制
void GpioBspLedStateSet(uint8_t io_stat)
{
	if(io_stat)
	{
		GPIO_LED->DSET = PIN_LED;
	}
	else
	{
		GPIO_LED->DRST = PIN_LED;
	}
}
